package com.cxk.hr.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cxk.hr.pojo.Employee;
import com.cxk.hr.pojo.History;
import com.cxk.hr.security.PermissionSign;
import com.cxk.hr.service.EmployeeService;
import com.cxk.hr.service.HistoryService;
import com.cxk.hr.util.MTimeUtil;

@Controller
@RequestMapping("/history")
public class HistoryController {
	@Autowired
	private HistoryService historyService;
	@Autowired
	private EmployeeService employeeService;

	@RequestMapping("/{id}/detail")
	@RequiresPermissions(value = PermissionSign.HISTORY_DETAIL)
	public String selectHistory(@PathVariable Integer id, Model model) {
		History history = historyService.selectHistoryById(id);
		model.addAttribute("history", history);
		return "admin/history_detail";
	}

	@RequestMapping("/{id}/toUpdate")
	@RequiresPermissions(value = PermissionSign.HISTORY_UPDATE)
	public String toUpdate(Model model, @PathVariable Integer id) {
		History history = historyService.selectHistoryById(id);
		if (history.getStatus().equals("在职")) {
			Employee employee = employeeService.selectByNumber(history.getEmployeeNumber());
			return "forward:/employee/" + employee.getId() + "/toUpdate";
		} else {
			model.addAttribute("history", history);
			return "admin/history_update";
		}
	}

	@RequestMapping("/{id}/update")
	public String updateById(@PathVariable Integer id, History history, String date) {
		history.setId(id);
		history.setBirthday(MTimeUtil.stringParse(date));
		historyService.updateById(history);
		return "forward:/history/listPage?pageNo=1";
	}

	@RequestMapping("/{id}/updateRetire")
	@RequiresPermissions(value = PermissionSign.HISTORY_UPDATE_SUBMIT)
	public String updateRetire(@PathVariable Integer id, History history, String date) {
		history.setId(id);
		history.setBirthday(MTimeUtil.stringParse(date));
		historyService.updateById(history);
		return "forward:/history/retireListPage?pageNo=1";
	}

	@RequestMapping("/list")
	@RequiresPermissions(value = PermissionSign.EMPLOYEE_LIST)
	public String list(Model model) {
		List<History> histories = historyService.selectList(0, 10000);
		model.addAttribute("histories", histories);
		return "admin/history_list";
	}

	@RequestMapping("/listPage")
	public String selectListByPage(Model model, Integer pageNo, Integer pageSize) {
		List<History> histories = historyService.selectList(pageNo, pageSize);
		model.addAttribute("histories", histories);
		return "admin/history_list";
	}

	@RequestMapping("/retireListPage")
	@RequiresPermissions(value = PermissionSign.HISTORY_LIST)
	public String selectRetireByPage(Model model, Integer pageNo, Integer pageSize) {
		List<History> histories = historyService.selectRetireList(pageNo, pageSize);
		model.addAttribute("histories", histories);
		return "admin/retire_list";
	}
}
